//
//  SISSBaseTableVC.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/10.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSBaseTableViewController.h"

@interface SISSBaseTableViewController ()

/* 是否没有网络 */
@property (assign, nonatomic) BOOL isNoNetwork;

@end

@implementation SISSBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubviews];
    [self setupNavigationItems];
    [self addRefresh];
    
//    self.tableView.emptyDataSetSource = self;
//    self.tableView.emptyDataSetDelegate = self;
//    
//    self.tableView.tableFooterView = [UIView new];
    
    // 注册网络变化的监听事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangedNotification:) name:kReachabilityChangedNotification object:nil];
    
}

#pragma mark - DZNEmptyDataSetSource,DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *imageName = self.isNoNetwork ? @"网络" : @"空白";
    
    return  [UIImage imageNamed:imageName];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = self.isNoNetwork ? @"糟糕~网络开小差了！" : @"";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:12.0f],
                                 NSForegroundColorAttributeName: COLOR(0x8998A5)};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (void)reachabilityChangedNotification:(NSNotification *)notification {
    
    NSLog(@"notification -> %@",notification.object);
    
    self.isNoNetwork = [notification.object intValue] == 0;
    [self.tableView reloadData];
}

- (void)dealloc {
    // 移除当前对象监听的事件
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupSubviews {

}

- (void)setupNavigationItems {
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //将键盘缩回去,让控制器所管理的UIView结束编辑
    [self.view endEditing:YES];
}





- (void)addRefresh {
    
    //设置mj_header
    __weak typeof(self) weakSelf = self;
    MJRefreshNormalHeader *header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf upDownReloadMoreData];
    }];
    self.tableView.mj_header  = header;
    
//    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf reloadMoreData];
//        
//    }];
//    self.tableView.mj_footer = footer;
    
    // 设置文字
        [header setTitle:@"刷新成功" forState:MJRefreshStateIdle];
        [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"数据刷新中 ..." forState:MJRefreshStateRefreshing];
    
    // 设置字体
    header.stateLabel.font = [UIFont systemFontOfSize:15];
    header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    
    // 设置颜色
    //    header.stateLabel.textColor = BASECOLOR;
    //    header.lastUpdatedTimeLabel.textColor = BASECOLOR;
}


#pragma mark - 下拉刷新
- (void)upDownReloadMoreData {
    
    
}

#pragma mark - 刷新加载更多的方法
- (void)reloadMoreData {
    
    //    __weak typeof(self) weakSelf = self;
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //
    //        //        [LivesTools livesShowSVSuccessTitle:@"加载成功" andTime:STANTIME andColor:YES];
    //        [weakSelf.mj_header endRefreshing];
    //
    //    });
    
}

#pragma mark - 加载更多的结束刷新

- (void)endHeaderRefresh {
    
    [self.tableView.mj_header endRefreshing];
}
- (void)endfooterRefresh {
    
    [self.tableView.mj_footer endRefreshing];
}
@end
